package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.*;
import com.qf.service.CourseService;
import com.qf.service.SpeakerService;
import com.qf.service.SubjectService;
import com.qf.service.VideoService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private SpeakerService speakerService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CourseService courseService;

    //展示视频信息
    @RequestMapping("showVideo")
    public String showVideo(Integer videoId, String subjectName, Model model) {

        Video video = videoService.findByVideoId(videoId);

        model.addAttribute("video", video);
        model.addAttribute("subjectName", subjectName);

        List<Subject> subjectList = subjectService.findAllSubject();

        model.addAttribute("subjectList", subjectList);

        Integer courseId = video.getCourseId();
        Course course = courseService.findCourseById(courseId);

        model.addAttribute("course", course);
        return "before/section.jsp";
    }

    //分页显示视频信息
    @RequestMapping("list")
    public String list(QueryVo queryVo, Model model,
                       @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                       @RequestParam(required = false, defaultValue = "10") Integer pageSize) {

        model.addAttribute("queryVo", queryVo);

        PageHelper.startPage(pageNum, pageSize);

        List<Video> videoList = videoService.findByQueryVo(queryVo);
        PageInfo<Video> videoPageInfo = new PageInfo<>(videoList);
        model.addAttribute("page", videoPageInfo);

        List<Speaker> speakerList = speakerService.findAll();
        model.addAttribute("speakerList", speakerList);

        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList", courseList);


        return "behind/videoList.jsp";
    }

    //跳转到添加/修改页面
    @RequestMapping("addVideo")
    public String addVideo(Model model) {

        List<Speaker> speakerList = speakerService.findAll();
        model.addAttribute("speakerList", speakerList);

        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList", courseList);

        return "behind/addVideo.jsp";
    }

    //添加/修改
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Video video) {
        if (video.getId() == null) {
            videoService.addVideo(video);
        } else {
            videoService.update(video);
        }

        return "redirect:list";
    }

    //根据ID删除视频
    @RequestMapping("videoDel")
    @ResponseBody
    public String videoDel(Integer id) {

        videoService.deleteVideo(id);
        Video video = videoService.findByVideoId(id);

        if (video == null) {
            return "success";
        } else {
            return "false";
        }
    }

    //批量删除视频
    @RequestMapping("delBatchVideos")
    public String delBatchVideos(Integer[] ids) {

        for (Integer id : ids) {
            videoService.deleteVideo(id);
        }

        return "redirect:list";
    }

    //跳转到修改视频界面
    @RequestMapping("edit")
    public String edit(Integer id, Model model) {

        Video video = videoService.findByVideoId(id);
        model.addAttribute("video", video);

        List<Speaker> speakerList = speakerService.findAll();
        model.addAttribute("speakerList", speakerList);

        List<Course> courseList = courseService.findAll();
        model.addAttribute("courseList", courseList);

        return "behind/addVideo.jsp";
    }

    //更新播放次数
    @RequestMapping("updatePlayNum")
    public void updatePlayNum(Video video) {

        Integer playNum = video.getPlayNum();

        video.setPlayNum(playNum + 1);

        videoService.update(video);
    }

}
